//
//  ZoneAlertSettingCollectionViewCell.m
//  EZ Tracker
//
//  Created by FENGYL on 2017/4/1.
//  Copyright © 2017年 FENGYL. All rights reserved.
//

#import "ZoneAlertSettingCollectionViewCell.h"
#import "UserModel.h"
#import "UIImage+RTTint.h"
#import "NewZoneViewController.h"
#import "ZoneAlertSettingView.h"

@interface ZoneAlertSettingCollectionViewCell()

@property (strong, nonatomic) UserModel *userModel;

@property (weak, nonatomic) IBOutlet UIButton *iconButton;
@property (weak, nonatomic) IBOutlet UILabel *serialNumberLabel;
@property (weak, nonatomic) IBOutlet UILabel *deviceNameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *yesImageView;


@end

@implementation ZoneAlertSettingCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self = [[[NSBundle mainBundle] loadNibNamed:@"ZoneAlertSettingCollectionViewCell" owner:self options:nil] lastObject];
    }
    return self;
}

-(void)setUpCellForSerialNumber:(NSString *)serialNumber deviceName:(NSString *)deviceName isSelected:(BOOL)isSelected{
    
    [self.serialNumberLabel setText:serialNumber];
    [self.deviceNameLabel setText:deviceName];
    
    self.iconButton.selected = isSelected;
    self.yesImageView.hidden = !isSelected;
    
}

-(void)setUpIconImage:(UIImage *)image{
    
    if (!image) {
        
        image = [UIImage imageNamed:@"ic_tracker_useractivity_yuan_2"];
        
    }
    
    [self.iconButton setImage:image forState:UIControlStateNormal];
    
    
    //按钮selected图片变灰
    UIImage *buttonImage = image;
    
    UIImage *highlightImage = [buttonImage rt_darkenWithLevel:0.5];
    
    [self.iconButton setImage:highlightImage forState:UIControlStateSelected];
    
}

#pragma mark 点击iconButton
- (IBAction)touchIconButton:(id)sender {
    
    self.iconButton.selected = !self.iconButton.selected;
    
    self.yesImageView.hidden = !self.yesImageView.hidden;
    
//    NewZoneViewController *newZoneViewController;
//    
//    for (UIView* next = [self superview]; next; next = next.superview) {
//        UIResponder *nextResponder = [next nextResponder];
//        if ([nextResponder isKindOfClass:[NewZoneViewController class]]) {
//            
//            newZoneViewController = (NewZoneViewController *)nextResponder;
//            
//            break;
//            
//        }
//    }
    
    
    
    ZoneAlertSettingView *zoneAlertSettingView;
    
    NSLog(@"cell superView %@",[self.superview class]);
    NSLog(@"cell superView superView superView %@",[self.superview.superview.superview class]);
    
    if ([self.superview.superview.superview isKindOfClass:[ZoneAlertSettingView class]]) {
        
        zoneAlertSettingView = (ZoneAlertSettingView *)self.superview.superview.superview;
        
    }
    
    NSString *serialNumberString = self.serialNumberLabel.text;
    
    if (self.iconButton.selected && ![zoneAlertSettingView.selectedDeviceArray containsObject:serialNumberString]) {
        
        [zoneAlertSettingView.selectedDeviceArray addObject:serialNumberString];
        
    }else{
        
        [zoneAlertSettingView.selectedDeviceArray removeObject:serialNumberString];
        
    }
    
}


@end
